import React, { Component, Fragment } from 'react';
import { connect } from 'dva';
import { Form, Row, Col, Input } from 'antd';
import moment from 'moment';
import StandardTable from '@/components/StandardTable';
import AdModal from '@/components/AdModal';
import AdButton from '@/components/AdButton';
import AdSearch from '@/components/AdSearch';
import RightDraw from '@/components/RightDraw';
import { queryDict } from '@/utils/common';
import { allDictList } from '@/utils/constans';
import { allDispatchType, selectInfoList, renderTableAdSelect, codes } from './utils';
import DeclareInfoDetail from './DeclareInfoDetail';
import FileReader from '@/components/FileReader';
import { formateDateToMin } from '@/utils/common';

@connect(({ declare, component, loading }) => ({
  mdsDeliveryList: declare.mdsDeliveryList,
  declareDetail: declare.declareDetail,

  dictObject: component.dictObject,
  loading: loading.effects[allDispatchType.mdsDeliveryList],
}))
@Form.create()
export default class DeclareInfo extends Component {
  state = {
    detailId: '',
    selectedRows: [],
    addSelectedRows: [],
    checkId: '',
    formValues: {},
    visible: false,
  };

  componentDidMount() {
   setTimeout(()=>{
    const { detailId } = this.props;
    this.setState({
      detailId,
    });
    // console.log('detailId--2---',detailId)
    this.getDeliveryList(detailId);
   },1000)
  }

  componentWillMount() {
    const allDict = [allDictList.manifest_current, allDictList.manifest_goods];
    queryDict({ props: this.props, allDict });
  }

  componentWillReceiveProps(nextProps, nextState) {
    const { detailId, declareDetail, visible } = nextProps;
    const detail = declareDetail[detailId];
    if (detailId && !nextState.detailId && detail) {
      this.handleStateChange([{ detailId: detail.id }]);
    }
    if (
      JSON.stringify(declareDetail) !== JSON.stringify(this.props.declareDetail) ||
      this.props.detailId !== detailId
    ) {
      if (detailId && detail) {
        this.handleStateChange([{ detailId: detail.id }]);
        this.getDeliveryList(detail.id);
      }
    }
  }

  getDeliveryList = detailId => {
    selectInfoList({
      type: allDispatchType.deliveryListPlus,
      payload: { roadManifestId: detailId },
      props: this.props,
    });
  };
  //详情
  showDetail = (checkId, recordStatus) => {
    this.handleStateChange([{ checkId }, { recordStatus }, { visibleDetail: true }]);
  };
  handleCandle = () => {
    this.setState({
      visibleDetail: false,
    });
  };

  renderInfo = (text, record) => {
    const { mode, open } = this.props;
    const _text = <a>{text}</a>;
    return (
      <AdButton
        mode="a"
        onClick={() => this.showDetail(record.id, record.status)}
        text={open ? _text : text}
        code={codes.showDetail}
      />
    );
  };

  handleStateChange = (options = []) => {
    options.map(item => {
      this.setState(item);
    });
  };
  columns = [
    {
      title: '提(运)单号',
      dataIndex: 'deliveryNo',
      render: (text, record) => this.renderInfo(text, record),
    },
    {
      title: '海关货物通关',
      dataIndex: 'goodsItemCode',
      render: text =>
        renderTableAdSelect({
          props: this.props,
          value: text,
          key: allDictList.manifest_goods,
          declare: true,
        }),
    },
    {
      title: '货物总件数',
      dataIndex: 'goodsTotal',
    },
    {
      title: '货物总毛重(kg)',
      dataIndex: 'goodsGrossWeith',
    },
    {
      title: '货物价值',
      dataIndex: 'goodsCost',
    },
    {
      title: '收货人',
      dataIndex: 'consignee',
      render: text => <span title={text}>{text}</span>,
    },
    {
      title: '金额类型',
      dataIndex: 'currentCode',
      render: text =>
        renderTableAdSelect({
          props: this.props,
          value: text,
          key: allDictList.manifest_current,
          declare: true,
        }),
    },
    // {
    //   title: '创建人',
    //   dataIndex: 'createBy',
    //   render: text => (
    //     <AdSearch
    //       label="loginName"
    //       name="sysName"
    //       value={this.props.searchValue[text]}
    //       onlyRead={true}
    //     />
    //   ),
    // },
    // {
    //   title: '创建时间',
    //   dataIndex: 'createTime',
    //   render: text => <span>{text ? moment(text).format(formateDateToMin) : ''}</span>,
    // },
    // {
    //   title: '更新人',
    //   dataIndex: 'updateBy',
    //   render: text => (
    //     <AdSearch
    //       label="loginName"
    //       name="sysName"
    //       value={this.props.searchValue[text]}
    //       onlyRead={true}
    //     />
    //   ),
    // },
    // {
    //   title: '更新时间',
    //   dataIndex: 'updateTime',
    //   render: text => <span>{text ? moment(text).format(formateDateToMin) : ''}</span>,
    // },
  ];

  /**
   * table 表格 分页操作
   */
  handleStandardTableChange = param => {
    const { detailId } = this.state;
    selectInfoList({
      type: allDispatchType.mdsDeliveryList,
      payload: { roadManifestId: detailId, ...param },
      props: this.props,
    });
  };

  render() {
    const { mdsDeliveryList, loading, onSelectRow, mode, status, isMobile, pageType } = this.props;
    const { selectedRows, detailId, checkId, visibleDetail } = this.state;
    const rightDrawParams = {
      isMobile: isMobile,
      visible: visibleDetail,
      title: '提运单详情',
      widthX: '800px',
      code: codes.showDetail,
      closeDetail: this.handleStateChange.bind(this, [{ visibleDetail: false }]),
    };

    return (
      <Fragment>
        {visibleDetail && pageType && (
          <AdModal
            visible={visibleDetail}
            title={`明细详情`}
            // onOk={this.handleOk}
            onCancel={() => {
              this.handleCandle();
            }}
            width="80%"
            footer={null}
          >
            <DeclareInfoDetail detailId={checkId} />
          </AdModal>
        )}

        <StandardTable
          disabledRowSelected={mode === 'detail' || status}
          loading={loading}
          // selectedRows={selectedRows}
          // onSelectRow={selectedRows => {
          //   onSelectRow && onSelectRow(selectedRows);
          //   this.handleStateChange([{ selectedRows }]);
          // }}
          data={mdsDeliveryList[detailId] || {}}
          columns={this.columns}
          scrollX={2000}
          scrollY={200}
          code="BillInfo"
          onPaginationChange={this.handleStandardTableChange}
        />
        {!pageType && (
          <RightDraw {...rightDrawParams}>
            <DeclareInfoDetail detailId={checkId} />
          </RightDraw>
        )}
      </Fragment>
    );
  }
}
